package com.atguigu.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.vo.AttrAttrgroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import static java.util.stream.Collectors.toList;


/**
 * 属性&属性分组关联
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2024-11-03 10:05:39
 */
@RestController
@RequestMapping("product/attrattrgrouprelation")
public class AttrAttrgroupRelationController {
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{groupId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("groupId") Long groupId) {
        PageUtils page = attrAttrgroupRelationService.queryPage(params, groupId);
        final List<AttrAttrgroupRelationVo> collect =
                page.getList().stream().map(it -> attrAttrgroupRelationService.
                        getVo((AttrAttrgroupRelationEntity) it)).collect(toList());
        page.setList(collect);
        return R.ok().put("page", page);
    }

    @RequestMapping("/unbindList/{groupId}")
    public R unbindList(@RequestParam Map<String, Object> params,
                        @PathVariable("groupId") Long groupId) {

        final AttrGroupEntity attrGroupEntity = attrGroupService.getById(groupId);

        final PageUtils pageUtils = attrService.unbindList(attrGroupEntity.getCatelogId(), params);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        AttrAttrgroupRelationEntity attrAttrgroupRelation =
                attrAttrgroupRelationService.getById(id);

        return R.ok().put("attrAttrgroupRelation", attrAttrgroupRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrAttrgroupRelationEntity attrAttrgroupRelation) {
        attrAttrgroupRelationService.save(attrAttrgroupRelation);
        return R.ok();
    }
    @RequestMapping("/saves")
    @Transactional
    public R saves(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelations) {
        attrAttrgroupRelations.forEach(attrAttrgroupRelation -> {
            attrAttrgroupRelationService.save(attrAttrgroupRelation);
        });
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrAttrgroupRelationEntity attrAttrgroupRelation) {
        attrAttrgroupRelationService.updateById(attrAttrgroupRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody AttrAttrgroupRelationEntity[] AttrAttrgroupRelationEntities) {
        attrAttrgroupRelationService.removeBinds(Arrays.asList(AttrAttrgroupRelationEntities));
        return R.ok();
    }

}
